<?php
namespace app\index\controller;
/**
 * 单击版webview
 */
class Single extends Common{
    /**
     * 基本的公共信息
     * @return [type] [description]
     */
    function index(){
        $w=getWeek();
        $weekTime=$w[0];
        $weekEndTime=$w[1];

        $domain=request()->domain();
        $maxVipMoney=model('vip')->where(['status'=>1])->max('money');
        // 获取新手优惠卷数据列表
        $coupons=model('coupon')->readAll(['type'=>0,'status'=>1],0,['field'=>'title,usable,remark,value']);
        $rs=[
            'week'=>[date('W'),date('m.d',$weekTime).'-'.date('m.d',$weekEndTime-1)], //本周是第几周
            'week_bonus'=>config('setting.week_bonus'), //周奖金
            'week_standard'=>config('setting.week_standard'), //周达标金额（达标后可领奖金 ，week_bonus）

            'coupons'=>$coupons,
            'max_vip_money'=>$maxVipMoney,
            'feature'=>$domain.'/tagsicon/feature.jpg', 
            'zhuanqian_icon1'=>$domain.'/tagsicon/zhuanqian_icon1.png', 
            'zhuanqian_icon3'=>$domain.'/tagsicon/zhuanqian_icon3.png', 
        ];
        // 最高领取奖励百分比
        $rs['max_ratio']=model('vip')->where(['status'=>1])->max('ratio');
        return json('',0,$rs);
    }
    /**
     * 版本号
     * @return [type] [description]
     */
    function version(){
        $v=config('setting.weishou_version');
        $url=config('setting.weishou_app');
        return json($v,0,['version'=>$v,'url'=>$url]);
    }
    
    // 帮助中心
    function help(){
        $options=[
            'with'=>'tag',
            'field'=>'id,title,tag_id'
        ];
        $list=model('article')->readAll(['cate_id'=>1],0,$options)->toArray();
        $rs=[];
        $prefix=request()->domain().'/tagsicon/';
        foreach ($list as $v) {
            if(isset($rs[$v['tag_id']])){
                $rs[$v['tag_id']]['children'][]=$v;
            }else{
                $icon=empty($v['tag_icon'])?$v['tag_icon']:$prefix.$v['tag_icon'].'.png';
                $rs[$v['tag_id']]=[
                    'id'=>$v['tag_id'],
                    'name'=>$v['tag'],
                    'icon'=>$icon,
                    'children'=>[$v],
                ];
            }
        }
        $this->assign('list',$rs);
        return view();
    }
    // 帮助中心详情文章
    function helpDetail($id=0,$showBack=1,$showTitle=1){
        $article=model('article')->read(['id'=>$id]);
        $this->assign('article',$article);
        if($article['cate_id']!=1){
            $showBack=0;
            $showTitle=0;
        }
        $this->assign('showBack',$showBack);
        $this->assign('showTitle',$showTitle);
        return view();
    }
    // 分享注册页面
    function share($id=0){
        $member=model('member')->read(['id'=>$id],'id,tel');
        if(empty($member)){
            $member['id']=0;
        }
        if(request()->isAjax()||request()->isPost()){
            $tel=input('tel');
            $data=[
                'tel'=>input('tel')
            ];
            $validate=$this->validate($data,[
                'tel|电话号码'=>'require|mobile',
            ]);
            if(true!==$validate){
                $this->error($validate);
            }
            $hasTel=model('member')->where(['tel'=>$data['tel']])->count();
            if($hasTel){
                $this->error('已经领过礼包，请勿重复领取');
                die;
            }
            $this->_register($tel,$member['id'],false);
            $this->success('');
            die;
        }
        if(empty($member['tel'])){
            $member['tel']=13456789101;
        }
        $member['tel']=str_replace(substr($member['tel'], 3,4), '****', $member['tel']);
        $this->assign('member',$member);
        return view();
    }
    function _register($tel,$pid=0,$checkTel=true,$deviceId=null){
        if($checkTel){
            $has=model('member')->read(['tel'=>$tel],'id,code,status,deviceId');
            if(!empty($has)){
                $device_max=config('setting.device_max');
                if($device_max>0&&!is_null($deviceId)){
                    $deviceIdArr=json_decode($has['deviceId'],true);
                    if(empty($deviceIdArr)){
                        model('member')->edit([
                            'deviceId'=>json_encode([$deviceId])
                        ],['id'=>$has['id']]);
                        return success('',0,$has);
                    }elseif(!in_array($deviceId, $deviceIdArr)&&count($deviceIdArr)>=$device_max){
                        return error("每个账号仅允许最多绑定{$device_max}台设备",1);
                    }elseif(!in_array($deviceId, $deviceIdArr)&&count($deviceIdArr)<$device_max){
                        array_push($deviceIdArr, $deviceId);
                        model('member')->edit([
                            'deviceId'=>json_encode($deviceIdArr)
                        ],['id'=>$has['id']]);
                        return success('',0,$has);
                    }
                }
                return success('',0,$has);
            }
        }
        $days = 7;
        $data=[
            'tel'=>$tel,
            'name'=>$tel,
            'pid'=>$pid,
            'module_id'=>1,
            'status'=>1,
            's_time'=>time(),
            'trial_member'=>strtotime("+$days day",strtotime(date("Y-m-d"))),
        ];
        if(!is_null($deviceId)){
            $data['deviceId1']=$deviceId;
        }
        $add=model('member')->add($data);
        // 关联默认角色
        $defaultRole=model('role')->readAll(['is_default'=>1],0,['field'=>'id']);
        if(!empty($add)&&!empty($defaultRole)){
            $r=[];
            foreach ($defaultRole as $v) {
                $r[]=[
                    'role_id'=>$v['id'],
                    'member_id'=>$add['id'],
                ];
            }
            if(count($r)){
                model('role_member')->add($r);
            }
        }
        if(empty($add)){
            return $this->_register($tel,$pid);
        }
        return success('',0,$add);
    }

    /**
     * 获取所有省份、市辖区
     * @return [type] [description]
     */
    function province(){
        $list=model('district')->readAll(['level'=>['level','<=',1],'id'=>['id','<>',35],],0,['field'=>'id,parent_id,name,suffix'])->toArray();
        $list=node_merge($list,0,'parent_id');
        return json('',0,$list);
    }
    /**
     * 获取所有的行业列表
     * @return [type] [description]
     */
    function vocation(){
        $list=model('vocation')->readAll();
        return json('',0,$list);
    }
    /**
     * 获取所有的标签属性
     * @return [type] [description]
     */
    function attr(){
        $list=model('testattr')->readAll();
        return json('',0,$list);
    }
    /**
     * 获取微手轮播图
     * @return [type] [description]
     */
    function carousel(){
        $options=[
            'field'=>'id,title,link,is_blank,image',
            'order'=>'sort'
        ];
        $list=model('carousel')->readAll(['module_id'=>1],0,$options);
        foreach ($list as $k => $v) {
            $list[$k]['image']=images($v['image']);
        }
        $news=db('news')->where([
            'content'=>['content','not like','%试用']
        ])->order('id desc')->limit(0,5)->field('content')->select();
        $newStr='';
        foreach ($news as $v) {
            $newStr.=$v['content'];
        }
        return json('',0,[
            'carousel'=>$list,
            'news'=>$newStr
        ]);
    }
	
	function share1(){
        return view();
    }
    
}